<template>
    <div class="execution todoTasks">
        <basic-container>
            <avue-crud ref="crud"
                :page.sync="page"
                :data="tableData"
                :permission="permissionList"
                :table-loading="tableLoading"
                :option="tableOption"
                @on-load="getList"
                @search-change="searchChange"
                @refresh-change="refreshChange"
                @size-change="sizeChange"
                @current-change="currentChange"
                @row-update="handleUpdate"
                @row-save="handleSave"
                @row-del="rowDel">
<!--                <template slot="menuLeft" >-->
<!--                    <el-button  type="primary" size="small"  @click="addqj()">新增请假</el-button>-->
<!--                </template>-->
                <template slot="menu"  slot-scope="scope">
                    <el-button size="small"  type="text" icon="el-icon-edit-outline" @click="setParams(scope.row)">办理</el-button>
                    <el-button size="small"  type="text" icon="el-icon-view" @click="uploadDeployInfo(scope.row)">流程图</el-button>
                    <el-button size="small"  type="text" icon="el-icon-view" @click="chakanxiangqing(scope.row)">查看</el-button>
                </template>
            </avue-crud>
        </basic-container>
        <viewlook ref="Viewlook"></viewlook>
        <viewlookbudget ref="Viewlookbudget"></viewlookbudget>
        <viewlookbudgetedit ref="Viewlookbudgetedit"></viewlookbudgetedit>
        <viewlookbudgetadd ref="Viewlookbudgetadd"></viewlookbudgetadd>
        <viewlookbudgetaddedit ref="Viewlookbudgetaddedit"></viewlookbudgetaddedit>
        <viewlookbudgetamend ref="Viewlookbudgetamend"></viewlookbudgetamend>
        <flow-chart ref="flowChart" ></flow-chart>
        <history-routes ref="historyRoutes" style="display: none"></history-routes>
    </div>
</template>

<script>
    import {fetchList, getObj, addObj, putObj, delObj} from '@/views/wy-flow/api/flowbusimain-daiban'
    import {tableOption} from '@/views/wy-flow/const/flowbusimain-daiban'
    import {mapGetters} from 'vuex'
    import flowChart from './flow-chart'
    import axios from 'axios'
    import dbl from '@/utils/dbl'
    import Viewlook from "../flowbusimain/view-look";
    import Viewlookbudget from "../flowbusimain/view-lookbudget";
    import Viewlookbudgetedit from "../flowbusimain/view-lookbudgetedit";
    import Viewlookbudgetadd from "../flowbusimain/view-lookbudgetadd";
    import Viewlookbudgetaddedit from "../flowbusimain/view-lookbudgetaddedit";
    import Viewlookbudgetamend from "../flowbusimain/view-lookbudgetamend";
    import historyRoutes from '@/layout/history-routes'
    import {getBudgetApply} from "@/views/wy-budget/api/budgetapply.js";

    export default {
        name: 'flowbusimain-daiban',
        components: {flowChart,Viewlook,Viewlookbudget,Viewlookbudgetedit,Viewlookbudgetadd,Viewlookbudgetaddedit,Viewlookbudgetamend,historyRoutes},
        data() {
            return {
                searchForm: {},
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.flowbusimain_edit, false),
                    delBtn: this.vaildData(this.permissions.flowbusimain_del, false),
                    editBtn: this.vaildData(this.permissions.flowbusimain_edit, false)
                };
            }
        },
        methods: {
            chakanxiangqing(row) {
                if(row.busiTypeCode == '009'){
                    this.$refs.Viewlook.show(row.busiId);
                }else if(row.busiTypeCode == '001'){
                    getBudgetApply(row.busiId).then(respData => {
                        if(respData.data.data){
                            if(respData.data.data.isEdit == '1'){
                                this.$refs.Viewlookbudgetedit.show(row.busiId);
                            }else{
                                this.$refs.Viewlookbudget.show(row.busiId);
                            }
                        }else{
                            this.$message.success('无业务数据');
                        }
                    }).catch(e => {
                    })
                }else if(row.busiTypeCode == '004'){
                    getBudgetApply(row.busiId).then(respData => {
                        if(respData.data.data){
                            if(respData.data.data.isEdit == '1'){
                                this.$refs.Viewlookbudgetamend.show(row.busiId);
                            }else{
                                this.$refs.Viewlookbudgetamend.show(row.busiId);
                            }
                        }else{
                            this.$message.success('无业务数据');
                        }
                    }).catch(e => {
                    })
                }else if(row.busiTypeCode == '005'){
                    getBudgetApply(row.busiId).then(respData => {
                        if(respData.data.data){
                            if(respData.data.data.isEdit == '1'){
                                this.$refs.Viewlookbudgetaddedit.show(row.busiId);
                            }else{
                                this.$refs.Viewlookbudgetadd.show(row.busiId);
                            }
                        }else{
                            this.$message.success('无业务数据');
                        }
                    }).catch(e => {
                    })
                }
            },

            setParams(data){//点击跳转函数
                const config = {
                        headers: { 'Authorization': dbl.get('token'), }
                    }

                axios.get("/api/flowbusimain/getIsLock/"+data.busiId,config).then(response => {
                    if(response.data.msg==null){
                        // 锁定任务
                        this.doTaskLock(data.busiId)
                        //this.$refs.historyRoutes.delTab('/wy-flow/daiban-list')
                        if(data.busiTypeCode == '009'){
                            this.$router.push({
                                path:'daiban-list2',//跳转路由
                                query:{//参数对象
                                    busiIds:data
                                }
                            });
                        }else if(data.busiTypeCode == '001'){
                            // 待受理 编辑页面
                            if(data.busiState == '1000'){
                                this.$router.push({
                                    path:'daiban-budget-apply2',//跳转路由
                                    query:{//参数对象
                                        busiIds:data
                                    }
                                });
                            }

                            // 待审批
                            if(data.busiState == '1200'){
                                // 预算申请业务逻辑
                                getBudgetApply(data.busiId).then(respData => {
                                    if(respData.data.data.isEdit == '1'){
                                        this.$router.push({
                                            path:'daiban-budget-check2',//跳转路由
                                            query:{//参数对象
                                                busiIds:data
                                            }
                                        });
                                    }else{
                                        this.$router.push({
                                            path:'daiban-budget-check',//跳转路由
                                            query:{//参数对象
                                                busiIds:data
                                            }
                                        });
                                    }
                                }).catch(e => {
                                })
                            }
                            // 审批不通过
                            if(data.busiState == '1202'){
                                this.$router.push({
                                    path:'daiban-budget-apply2',//跳转路由
                                    query:{//参数对象
                                        busiIds:data
                                    }
                                });
                            }
                        }else if(data.busiTypeCode == '004'){
                            // 判断进入哪个页面
                            // 待受理 编辑页面


                            if(data.busiState == '1000'){
                                this.$router.push({
                                    path:'daiban-budget-amend',//跳转路由
                                    query:{//参数对象
                                        busiIds:data
                                    }
                                });
                            }

                            // 待审批
                            if(data.busiState == '1200'){
                                // 预算申请业务逻辑
                                getBudgetApply(data.busiId).then(respData => {
                                    this.$router.push({
                                        path:'daiban-budget-amendcheck',//跳转路由
                                        query:{//参数对象
                                            busiIds:data
                                        }
                                    });
                                }).catch(e => {
                                })
                            }
                            // 审批不通过
                            if(data.busiState == '1202'){
                                this.$router.push({
                                    path:'daiban-budget-amend',//跳转路由
                                    query:{//参数对象
                                        busiIds:data
                                    }
                                });
                            }
                        }else if(data.busiTypeCode == '005') {

                            // 审批不通过
                            if(data.busiState == '1000'){
                                this.$router.push({
                                    path:'daiban-budgetadd-edit',//跳转路由
                                    query:{//参数对象
                                        busiIds:data
                                    }
                                });
                            }

                            // 待审批
                            if(data.busiState == '1200'){
                                // 预算申请业务逻辑
                                getBudgetApply(data.busiId).then(respData => {
                                    if(respData.data.data.isEdit == '1'){
                                        this.$router.push({
                                            path:'daiban-budgetadd-check2',//跳转路由
                                            query:{//参数对象
                                                busiIds:data
                                            }
                                        });
                                    }else{
                                        this.$router.push({
                                            path:'daiban-budgetadd-check',//跳转路由
                                            query:{//参数对象
                                                busiIds:data
                                            }
                                        });
                                    }
                                }).catch(e => {
                                })
                            }
                            // 审批不通过
                            if(data.busiState == '1202'){
                                this.$router.push({
                                    path:'daiban-budgetadd-edit',//跳转路由
                                    query:{//参数对象
                                        busiIds:data
                                    }
                                });
                            }
                        }

                    }else{
                        this.$message.error(response.data.msg)
                        this.getList(this.page)
                    }
                }).catch(e => {
                })

            },
            //  锁定 任务
            doTaskLock(busiId){
                const config = {
                    headers: { 'Authorization': dbl.get('token'),
                    }
                };
                axios.get("/api/flowbusimain/doTaskLock/"+busiId,config).then(response => {
                }).catch(e => {
                })
            },
            addqj(){
                const config = {
                    headers: { 'Authorization': dbl.get('token')}
                }
                axios.post("/api/flowleaveapply",config).then(response => {
                    if(response.data.data.msg=="操作成功"){
                        this.$message.success(response.data.data.msg)
                    }else{
                        this.$message.error(response.data.data.msg)
                    }
                    this.getList(this.page)
                }).catch(e => {
                })
            },
            uploadDeployInfo(data) {
                this.$refs.flowChart.show(data)
            },
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({

                    current: page.currentPage,
                    size: page.pageSize
                }, params, this.searchForm )).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            rowDel: function (row, index) {
                this.$confirm('是否确认删除?' , '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.busiId)
                }).then(data => {
                    this.$message.success('删除成功')
                    this.getList(this.page)
                })
            },
            handleUpdate: function (row, index, done,loading) {
                putObj(row).then(data => {
                    this.$message.success('修改成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            handleSave: function (row, done,loading) {
                addObj(row).then(data => {
                    this.$message.success('添加成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            currentChange(current){
                this.page.currentPage = current
            },
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            refreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>
<style lang="scss" scoped>
    .todoTasks{
        .basic-container{
            padding-top: 30px;
        }
        /deep/ .avue-crud__search{
            .avue-form__group .el-col:first-child{
                padding-left: 0 !important;
                 .el-form-item__label{
                     padding: 0;
                 }
            }
        }
        .el-form-item__label{
            width: 90px !important;
        }
        .el-form-item__content{
            margin-left: 100px !important;
        }
        .avue-crud__search .el-form-item--small.el-form-item{
            width: 300px;
        }

    }
</style>
